#!/usr/bin/perl -w
#
# Display info about a Person, with the option to
# edit (if you have the proper key)
#
use strict;
use LinuxCounter;
use FileTable;
use Counter;
use Fcntl;
use CGI;
use CGI::Carp;
use ErrorForm;
use CounterCGI;
use Malus;

use vars qw($q $rec $key $users $persons $userrec $privacy $mail);


# DEBUG statement - movable
print "Content-type: text/plain\n\ndisplay-person stopping now\n";
exit 0;


$q = new CGI;
# Make sure things go Just A Bit Slowly if mass-grab occurs
Malus::note("display-person", $q->remote_host());
my $mstatus = Malus::punish("display-person", $q->remote_host());
if ($mstatus > 1) {
    warn "Showing error page\n";
    # Angry at the guy!
    my $ip = $q->remote_host();
    print <<EoF;
Content-type: text/html; charset="utf-8"

<html>
<head>
<title>Internal error - please report</title>
</head>
<h1>Internal error</h1>
There has been an internal processing problem.
<p>
Please report the following to webmaster\@counter.li.org:
<p>
ID: $0 Malus<br>
C: $ip
<p>
</html>
EoF
    exit 0;
}
$users = Counter::open(O_RDONLY);
$persons = Counter::openfile("persons", O_RDONLY);
$key = $q->param('user');
$rec = $persons->getclean($key);
if (!$rec) {
    errorForm("Record $key does not exist");
}
$userrec = $users->get($key);
if (!$userrec) {
    errorForm("No user record for $key");
}
#
# For backwards compatibility with privacy rules
#
$privacy = $userrec->privacy();

if ($privacy eq "none") {
    # override for the case of a bad email
    my @mail = $userrec->emails("main");
    if ($mail[0]->{state} ne "bad") {
	warn "$key: privacy $privacy mail $$mail{state}\n";
	errorForm("private info", $key);
    } else {
	print <<EoF;
Content-type: text/html; charset="utf-8"

<html><head>
<title>Linux Counter User With Bad Email</title>
</head>
<body bgcolor="black" text="white" link="yellow">
<h1>Linux Counter user $key has bad email</h1>
This user has not made his information public.
<p>
However, his contact email, $$userrec{email}, is not reachable.
<p>
If you can suggest another email he can be reached by, type it below.
<form method="POST" action="/cgi-bin/runscript/not-yet-done.cgi">
<input name="newmail" size=60>
<input type="hidden" name="key" value="$key">
<input type="submit" value="Submit">
</form>
</body>

EoF
    }
    exit(0);
}


#------------ back to being sensible....

print <<EoF;
Content-type: text/html; charset="utf-8"

<html>
<head>
<title>Linux Counter Information for $$rec{name}</title>
</head>
<body bgcolor="white">
<h1>$$rec{name}</h1>
<img src="/cgi-bin/certificate.cgi/$key">
<p>
This Linux User has entered the following information
with the Linux Counter:
<p>
EoF
    printPersonTable($rec);
    print <<EoF;
<p>
The record ID is $key
<hr>
Record last updated: $$rec{recdate}<br>
EoF

print <<EoF;
<hr>
To change this record, <a href="/login.php">log in</a> as user $key.
<hr>
</body>
</html>
EoF

